<template>
  <el-dialog title="委外收货结算详细" :close-on-click-modal="false" :visible.sync="visible" center fullscreen>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" ref="dataForm" label-width="120px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="单据编号">
            <el-input v-model="dataForm.billId" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="订单编号">
            <el-input disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="单据日期">
            <el-input v-model="dataForm.createTime" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="所属制造商">
            <el-input v-model="dataForm.supplyName" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="入库类型">
            <el-input v-model="dataForm.inveType" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="接收仓位">
            <el-input v-model="dataForm.inStockName" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="制单人">
            <el-input v-model="dataForm.createUserName" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="入库审核人">
            <el-input v-model="dataForm.auditUserName" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="运单号">
            <el-input v-model="dataForm.transportBillId" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="memo">
            <el-input type="textarea" :rows="2" disabled v-model="dataForm.memo"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table :data="dataForm.list" border v-loading="dataListLoading" header-cell-class-name="miui-header-cell-style"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="goodsCode" label="商品代码"></el-table-column>
      <el-table-column prop="goodsName" label="商品名称"></el-table-column>
      <el-table-column prop="goodsSpec" label="型号"> </el-table-column>
      <el-table-column prop="price" label="零售价"></el-table-column>
      <el-table-column prop="goodsQuantity" label="采购数量"></el-table-column>
      <el-table-column prop="notTaxPrice" label="单位成本"></el-table-column>
      <el-table-column prop="notTaxPrice" label="成本合计"></el-table-column>
      <el-table-column prop="taxRate" label="税率"></el-table-column>
      <el-table-column prop="taxPrice" label="含税单价"></el-table-column>
      <el-table-column prop="taxRatePrice" label="税额合计"></el-table-column>
      <el-table-column prop="taxPrice" label="价税合计"></el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {
    receiveApplyGetObj,
    receiveApplyAddObj,
    receiveApplyPutObj
  } from '@/api/eims/purchase/receive-apply.js'
  import { getConsignSettleById } from '@/api/eims/consign/consignSettlement.js'
  import { fetchCtgList } from '@/api/eims/ctg.js'
  export default {
    data() {
      return {
        visible: false,
        calculate: '1',
        dataForm: {
          id: 0,
          applyStatus: '2',
          ctgId: '',
          ctgName: '',
          supplyId: '',
          supplyName: '',
          memo: '',
          inveStatus: '2',
          list: []
        },
        ctgOptions: [],
        dataList: [],
        dataListLoading: false,
        dataRule: {
          ctgId: [{
            required: true,
            message: '请选择采购类别'
          }],
          supplyName: [{
            required: true,
            message: '请选择制造商'
          }]
        },
        supplySearchVisible: false,
        goodsListVisible: false,
        selectionList: []
      }
    },
    methods: {
      init(id) {
        this.getCtgList()
        this.dataForm.id = id || 0
        this.dataForm.supplyId = ''
        this.visible = true
        this.dataList = []
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getConsignSettleById(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 商品类别改变时获取名字
      ctgSelectChange($event) {
        this.ctgOptions.forEach(item => {
          if (item.value == $event) {
            this.dataForm.ctgName = item.name
          }
        })
      },
      // 打开制造商列表弹窗
      checkSupplyList() {
        this.supplySearchVisible = true
        this.$nextTick(() => {
          this.$refs.supplySearch.init(this.dataForm.supplyId)
        })
      },
      // 选择制造商
      renewFormData(data) {
        this.dataForm.supplyId = data.id
        this.dataForm.supplyName = data.name
      },
      // 打开商品列表弹窗
      addGoodsBySingle() {
        // 判断是否选中采购类型
        if (!this.dataForm.ctgId) {
          this.$message.warning('请选择采购类型')
          return
        }
        // 判断是否选中制造商
        if (!this.dataForm.supplyId) {
          this.$message.warning('请选择制造商')
          return
        }
        let params = {
          ctgId: this.dataForm.ctgId,
          supplyId: this.dataForm.supplyId
        }
        let data = {
          params: params,
          data: this.dataList
        }
        this.goodsListVisible = true
        this.$nextTick(() => {
          this.$refs.sinleGoodsList.init(data)
        })
      },
      handleSelectionChange(val) {
        this.selectionList = val
      },
      // 删除选中的商品
      delTableData() {
        let data = this.selectionList
        data.forEach(item => {
          this.dataList.splice(item, 1)
        })
      },
      //更新表单数据
      updateFormData(data) {
        this.dataList = data
        let list = []
        data.forEach(item => {
          list.push({
            goodsId: item.id
          })
        })
        this.dataForm.list = list
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.dataForm.list.forEach((item, index) => {
              item.applyQuantity = this.dataList[index].applyQuantity
            })
            receiveApplyAddObj(this.dataForm).then(response => {
              if(response.data.code == '0'){
                (this.dataForm.applyStatus == '1') ? this.$message.success('新增并审核成功'): this.$message.success(
                  '新增成功')
                this.visible = false
                this.$emit('refreshDataList')
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },

      // 获取采购类型列表
      getCtgList() {
        fetchCtgList().then(result => {
          let dataList = result.data.data.records
          let list = []
          dataList.forEach(item => {
            list.push({
              value: item.id,
              name: item.name
            })
          })
          this.ctgOptions = list
        })
      }
    }
  }
</script>
